package com.utils.socket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Desc：socket测试
 * @Title：TestSocket
 * @author: zhaoyangyang
 * @version: 1.0
 * @date: 2017年4月21日 下午12:24:58
 */
public class ServerSocketTest {
    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(9091);
        try {
            Socket client = server.accept();
            try {
                BufferedReader input =
                        new BufferedReader(new InputStreamReader(client.getInputStream()));
                boolean flag = true;
                int count = 1;

                while (flag) {
                    System.out.println("客户端要开始发骚了，这是第" + count + "次！");
                    count++;

                    String line = input.readLine();
                    System.out.println("客户端说：" + line);

                    if (line.equals("exit")) {
                        flag = false;
                        System.out.println("客户端不想玩了！");
                    } else {
                        System.out.println("客户端说：" + line);
                    }

                }
            } finally {
                client.close();
            }

        } finally {
            server.close();
        }
    }

}
